import requests
from lxml import etree
from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
import re

base_url = 'https://weixin.sogou.com/weixin'
ops = Options()
# ops.add_argument('--proxy-server=http://112.87.69.76:9999')
driver = webdriver.Chrome(chrome_options=ops)
# cookie = {'CXID':'335758DBAA09C6D3619B005FA0EFD796','SUID':'92B2EC744C238B0A5D562CD800030EA6', 'wuid':'AAHrYIU9KwAAAAqLK1fvowsAQAU=','sw_uuid':'290993065','sg_uuid':'195904866','ssuid':'3277789135','IPLOC':'CN3100','ABTEST':'3|1581328287|v1','weixinIndexVisited':'1', 'SUV':'0022E8AA74ECB2925E4127A0F8FDC328','JSESSIONID':'aaal21CBQWm89hrVL7r_w','PHPSESSID':'k5abef0ftvtb9ttvfr28ho2of7','SNUID':'577729B2C5C35B821F46BFA4C65114C1','sct':'4', 'ppinf':'5|1581412500|1582622100|dHJ1c3Q6MToxfGNsaWVudGlkOjQ6MjAxN3x1bmlxbmFtZToxODolRTUlODAlOTglRTglOEIlQTV8Y3J0OjEwOjE1ODE0MTI1MDB8cmVmbmljazoxODolRTUlODAlOTglRTglOEIlQTV8dXNlcmlkOjQ0Om85dDJsdUJnMGo0LXRGZUJPSVpIVERMTEVVcDBAd2VpeGluLnNvaHUuY29tfA','pprdig':'jWxJOoKEyk7ACwySbKh9MXQ1C6_PF4rXd6ZWPl_eQR8sKuAHLmDMXMDZmg2q6SIjit8kEN4erI0P6Z_aHeTkS-sURe5HqK-Kg1IcinYAa6MWMfbO1c04uIhSsdLSLJY-ytvNxtiYzfSU-8YZsMux2t5AGMpy7ZZhcp1VJacHQWg;','sgid':'22-45738765-AV5CcJTDkDeeRldQHqfAOxg;','ppmdig':'1581412501000000646f6f41dc803664f9d0872aca8e2601',}
# driver.add_cookie(cookie_dict=cookie)

# 登录之后可以查看10页之后的数据
driver.get(base_url)
cookie = {'name':'CXID','value':'335758DBAA09C6D3619B005FA0EFD796'}
driver.add_cookie(cookie)
cookie = {'name':'SUID','value':'92B2EC744C238B0A5D562CD800030EA6'}
driver.add_cookie(cookie)
cookie = {'name':'wuid','value':'AAHrYIU9KwAAAAqLK1fvowsAQAU='}
driver.add_cookie(cookie)
cookie = {'name':'sw_uuid','value':'290993065'}
driver.add_cookie(cookie)
cookie = {'name':'sg_uuid','value':'195904866'}
driver.add_cookie(cookie)
cookie = {'name':'ssuid','value':'3277789135'}
driver.add_cookie(cookie)
cookie = {'name':'IPLOC','value':'CN3100'}
driver.add_cookie(cookie)
cookie = {'name':'ABTEST','value':'3|1581328287|v1'}
driver.add_cookie(cookie) 
cookie = {'name':'weixinIndexVisited','value':'1'}
driver.add_cookie(cookie)
cookie = {'name':'SUV','value':'0022E8AA74ECB2925E4127A0F8FDC328'}
driver.add_cookie(cookie)
cookie = {'name':'JSESSIONID','value':'aaal21CBQWm89hrVL7r_w'}
driver.add_cookie(cookie)
cookie = {'name':'PHPSESSID','value':'k5abef0ftvtb9ttvfr28ho2of7'}
driver.add_cookie(cookie)
cookie = {'name':'SNUID','value':'577729B2C5C35B821F46BFA4C65114C1'}
driver.add_cookie(cookie)
cookie = {'name':'sct','value':'4'}
driver.add_cookie(cookie)
cookie = {'name':'ppinf','value':'5|1581412500|1582622100|dHJ1c3Q6MToxfGNsaWVudGlkOjQ6MjAxN3x1bmlxbmFtZToxODolRTUlODAlOTglRTglOEIlQTV8Y3J0OjEwOjE1ODE0MTI1MDB8cmVmbmljazoxODolRTUlODAlOTglRTglOEIlQTV8dXNlcmlkOjQ0Om85dDJsdUJnMGo0LXRGZUJPSVpIVERMTEVVcDBAd2VpeGluLnNvaHUuY29tfA'}
driver.add_cookie(cookie)
cookie = {'name':'pprdig','value':'jWxJOoKEyk7ACwySbKh9MXQ1C6_PF4rXd6ZWPl_eQR8sKuAHLmDMXMDZmg2q6SIjit8kEN4erI0P6Z_aHeTkS-sURe5HqK-Kg1IcinYAa6MWMfbO1c04uIhSsdLSLJY-ytvNxtiYzfSU-8YZsMux2t5AGMpy7ZZhcp1VJacHQWg'}
driver.add_cookie(cookie)
cookie = {'name':'sgid','value':'22-45738765-AV5CcJTDkDeeRldQHqfAOxg'}
driver.add_cookie(cookie)
cookie = {'name':'ppmdig','value':'1581412501000000646f6f41dc803664f9d0872aca8e2601'}
driver.add_cookie(cookie)
# 搜索的关键字
key_word = '肺炎'
# 输入框
driver.find_element_by_xpath('//*[@id="query"]').send_keys(key_word)
# 文章搜索按钮
driver.find_element_by_xpath('//*[@class="swz"]').click()
# 下一页按钮
next_page = driver.find_element_by_xpath('//*[@id="sogou_next"]')
while next_page:
    index = 0
    list_h3 = driver.find_elements_by_xpath('//*[@id="main"]/div[3]/ul/li/div[@class="txt-box"]/h3')
    for h3 in list_h3:
        h3.click()
        
        try:
            windows = driver.window_handles
            driver.switch_to.window(windows[1])
            title = driver.title
            url = driver.current_url
            print(title, url)
            time.sleep(20)
            driver.close()
        except:
            pass
        
        index += 1
        print(index)
        windows = driver.window_handles
        driver.switch_to.window(windows[0])
    next_page = driver.find_element_by_xpath('//*[@id="sogou_next"]')
    next_page.click()
    print('-'*10, '下一页', '-'*10)

index = 0
list_h3 = driver.find_elements_by_xpath('//*[@id="main"]/div[3]/ul/li/div[@class="txt-box"]/h3')
for h3 in list_h3:
    h3.click()
    try:
        windows = driver.window_handles
        driver.switch_to.window(windows[1])
        title = driver.title
        url = driver.current_url
        print(title, url)
        time.sleep(2)
        driver.close()
    except:
        pass

    index += 1
    print(index)
    windows = driver.window_handles
    driver.switch_to.window(windows[0])

# {'CXID':'335758DBAA09C6D3619B005FA0EFD796','SUID':'92B2EC744C238B0A5D562CD800030EA6', 'wuid':'AAHrYIU9KwAAAAqLK1fvowsAQAU=','sw_uuid':'290993065','sg_uuid':'195904866','ssuid':'3277789135','IPLOC':'CN3100','ABTEST':'3|1581328287|v1','weixinIndexVisited':'1', 'SUV':'0022E8AA74ECB2925E4127A0F8FDC328','JSESSIONID':'aaal21CBQWm89hrVL7r_w','PHPSESSID':'k5abef0ftvtb9ttvfr28ho2of7','SNUID':'577729B2C5C35B821F46BFA4C65114C1','sct':'4', 'ppinf':'5|1581412500|1582622100|dHJ1c3Q6MToxfGNsaWVudGlkOjQ6MjAxN3x1bmlxbmFtZToxODolRTUlODAlOTglRTglOEIlQTV8Y3J0OjEwOjE1ODE0MTI1MDB8cmVmbmljazoxODolRTUlODAlOTglRTglOEIlQTV8dXNlcmlkOjQ0Om85dDJsdUJnMGo0LXRGZUJPSVpIVERMTEVVcDBAd2VpeGluLnNvaHUuY29tfA','pprdig':'jWxJOoKEyk7ACwySbKh9MXQ1C6_PF4rXd6ZWPl_eQR8sKuAHLmDMXMDZmg2q6SIjit8kEN4erI0P6Z_aHeTkS-sURe5HqK-Kg1IcinYAa6MWMfbO1c04uIhSsdLSLJY-ytvNxtiYzfSU-8YZsMux2t5AGMpy7ZZhcp1VJacHQWg;','sgid':'22-45738765-AV5CcJTDkDeeRldQHqfAOxg;','ppmdig':'1581412501000000646f6f41dc803664f9d0872aca8e2601',}
